<?php
/**
 * 单人房间相关接口
 */

namespace App\Http\Controllers\App\V3;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

class SingleRoomController extends BaseController
{
    public function nobility(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        $weekRank = app('repo_single_room')->richRankThisWeek($room_id);
        $nobility_list = app('repo_single_room')->nobilityUserList($room_id);

        return Response::sendData(['rank_data' => $weekRank, 'nobility_data' => $nobility_list]);
    }

    public function songList(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        $result = app('repo_single_room')->getSongList($room_id);

        return Response::sendData($result);
    }

    public function addSong(Request $request)
    {
        $room_id = $request->input('room_id');
        $song_name = $request->input('song_name');
        $singer = $request->input('singer');
        $price = $request->input('price');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        if (!$song_name) {
            throw new BasicException(40063);
        }
        if (!$singer) {
            throw new BasicException(40064);
        }
        if ($price <= 0 || $price > 3000) {
            throw new BasicException(40046);
        }
        app('repo_single_room')->addSong($room_id, UID, $song_name, $singer, $price);

        return Response::sendData('success');
    }

    public function updateSong(Request $request)
    {
        $room_id = $request->input('room_id');
        $song_id = $request->input('song_id');
        $song_name = $request->input('song_name');
        $singer = $request->input('singer');
        $price = $request->input('price');
        if ($room_id <= 0 || $song_id <= 0 || !$song_name || !$singer) {
            throw new BasicException(10010);
        }
        if ($price <= 0 || $price > 3000) {
            throw new BasicException(40046);
        }
        app('repo_single_room')->updateSong($room_id, UID, $song_id, $song_name, $singer, $price);

        return Response::sendData('success');
    }

    public function deleteSong(Request $request)
    {
        $room_id = $request->input('room_id');
        $song_id = $request->input('song_id');
        if ($room_id <= 0 || $song_id <= 0) {
            throw new BasicException(10010);
        }
        app('repo_single_room')->deleteSong($room_id, UID, $song_id);

        return Response::sendData('success');
    }

    public function open(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        app('repo_single_room')->switch($room_id, UID, 1);

        return Response::sendData('success');
    }

    public function close(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        app('repo_single_room')->switch($room_id, UID, 0);

        return Response::sendData('success');
    }

    public function order(Request $request)
    {
        $room_id = $request->input('room_id');
        $song_id = $request->input('song_id');
        if ($room_id <= 0 || $song_id <= 0) {
            throw new BasicException(10010);
        }
        app('repo_single_room')->order($room_id, UID, $song_id);

        return Response::sendData('success');
    }

    public function orderList(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        $result = app('repo_single_room')->getOrderList($room_id);

        return Response::sendData($result);
    }

    public function updateOrder(Request $request)
    {
        $room_id = $request->input('room_id');
        $order_id = $request->input('order_id');
        if ($room_id <= 0 || $order_id <= 0) {
            throw new BasicException(10010);
        }
        app('repo_single_room')->updateOrder($room_id, $order_id, UID);

        return Response::sendData('success');
    }

    public function publish(Request $request)
    {
        $room_id = $request->input('room_id');
        $reward_name = $request->input('reward_name');
        $user_count = $request->input('user_count');
        $gifts = $request->input('gifts');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        if (!$reward_name) {
            throw new BasicException(40066);
        }
        if ($user_count <= 0 || $user_count > 5) {
            throw new BasicException(40061);
        }
        if (empty($gifts)) {
            throw new BasicException(40058);
        }
        $result = app('repo_single_room')->publishDraw($room_id, UID, $reward_name, $user_count, $gifts);

        return Response::sendData($result);
    }

    public function drawInfo(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        $result = app('repo_single_room')->drawInfo($room_id);

        return Response::sendData($result);
    }

    public function rankHistory(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        $result = app('repo_single_room')->rankHistory($room_id);

        return Response::sendData($result);
    }

    public function heart(Request $request)
    {
        $room_id = $request->input('room_id');
        if ($room_id <= 0) {
            throw new BasicException(10010);
        }
        $result = app('repo_single_room')->heart($room_id, UID);

        return Response::sendData($result);
    }

    public function drawResult(Request $request)
    {
        $draw_id = $request->input('draw_id');
        if ($draw_id <= 0) {
            throw new BasicException(10010);
        }
        $result = app('repo_single_room')->getDrawResult(UID, $draw_id);

        return Response::sendData($result);
    }

    public function giveGift(Request $request)
    {
        $draw_gift_id = $request->input('draw_gift_id');
        if ($draw_gift_id <= 0) {
            throw new BasicException(10010);
        }
        app('repo_single_room')->giveGift(UID, $draw_gift_id);

        return Response::sendData('success');
    }
}
